
package com.idealighter.game.games.shuihu.handler;

import com.idealighter.game.core.annotation.ResMsgHandler;
import com.idealighter.game.games.shuihu.ShuiHuPlayer;
import com.idealighter.game.games.shuihu.message.ReqExchangeChipsMsg;
import com.idealighter.game.message.ModuleMsgIdConstant;
import com.idealighter.game.message.core.ResMessage;
import com.idealighter.game.robot.common.PlayerPosition;
import com.idealighter.game.robot.core.Player;
import com.idealighter.game.robot.handler.ResMessageHandler;
import com.idealighter.utils.code.RandCodeUtil;

import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


@ResMsgHandler(ModuleMsgIdConstant.ShuiHu.FASTER_ENTER_TABLE)

public class ResFastEnterTableHandler implements ResMessageHandler {

  private static final Logger LOG = LoggerFactory.getLogger(ResFastEnterTableHandler.class);

  @Override
  public void action(Player player, ResMessage message) {
    // ResEnterTableMsg msg = (ResEnterTableMsg)message;
    player.position = PlayerPosition.TABLE;
    LOG.info("[水浒传]玩家[{}][{}]进入桌子[{}]成功", player.userName, player.playerId,
        player.table(player.playerId).getId());

    if (player.playerInfo.getGold() <= 0) {
      player.delayLogout();
    } else {
      exchangeChips(player);
      // 兑换筹码后，随机几秒开始水浒
      player.schedule(() -> ((ShuiHuPlayer) player).startShuiHu(), RandCodeUtil.random(15, 20),
          TimeUnit.SECONDS);
    }

  }

  /**
   * 进入桌子后兑换全部金币为筹码,然后马上准备.
   * 
   * @param player .
   */
  private void exchangeChips(Player player) {
    ReqExchangeChipsMsg exchangeChipsMsg = new ReqExchangeChipsMsg();
    exchangeChipsMsg.setGold((int) player.playerInfo.getGold());
    player.sendMsg(exchangeChipsMsg);
  }
}
